package com.icangya.Utils;

import com.icangya.DAO.*;
import com.icangya.domain.*;
import com.icangya.service.IMoneyService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class TaskUtil {

    org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ICashRegisterDAO iCashRegisterDAO;

    @Autowired
    IRechargeRecordDAO iRechargeRecordDAO;

    @Autowired
    IGainRecordDAO iGainRecordDAO;


    @Autowired
    IMoneyService iMoneyService;


    @Autowired
    IUserFootDAO iUserFootDAO;


//    @Scheduled(cron = "0 0/1 * * * ?") //一分钟执行一次
    @Scheduled(cron = "0 0 1 * * ?")
    public void gain() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();

        Timestamp timestamp=new Timestamp(date.getTime());

       double cashMoney= iCashRegisterDAO.getCashMoneyNow(timestamp);
       double rechargeMoney= iRechargeRecordDAO.getRechargeMoneyNow(timestamp);
       double gainMoney=rechargeMoney-cashMoney;
//        (double gainMoney, double expendMoney, double rechargeMoney

//        private  double gainMoney;  //实际收入
//        private  double expendMoney;//支出  584.2700000000001
//        private  double rechargeMoney;//充值  5010.060000000001

        GainRecord gainRecord=new GainRecord(gainMoney,cashMoney,rechargeMoney,timestamp);
        iMoneyService.saveGainRecord(gainRecord);
//        iGainRecordDAO.save(gainRecord);

        logger.info(">>>>>>>>>>>>>>>>>>>gain.............");
    }


    //    @Scheduled(cron = "0 0/1 * * * ?") //一分钟执行一次
    @Scheduled(cron = "0 10 0 * * ?")
    public void Odevity() {


        logger.info(">>>>>>>>>>>>>>>>>>>Odevity.............");
    }





//
//    @Scheduled(cron = "0 0 1 * * ?")
//    public void turntableUser() {
//            if(FootBall.MISSION_TURNTABLE==0){
//                //获取可领取的奖励
//                List<FbMissionAward> missionAwards = iMissionService.getMissionAwardStatus();
//                if(missionAwards!=null) {
//                    Random random = new Random();
//                    int result = random.nextInt(missionAwards.size());
//                    FbMissionAward missionAward = missionAwards.get(result);  //获取奖励信息
//
//                    FbTurntableRecord turntableRecord= iMissionService.getTurntableRecordBySystem(0,user.getOpenId());
//                    if(turntableRecord==null){
//                        //添加转盘记录
//                        //String nickName, String openId, long uid, String headImgUrl, long missionId, String missionContent, long awardId, String awardContent, int status
//                        FbTurntableRecord fbTurntableRecord=new FbTurntableRecord(user.getNickName(),user.getOpenId(),user.getId(),user.getHeadImgUrl(),0,
//                                "赠送体验",missionAward.getId(),missionAward.getAwardContent(),0,new Timestamp(new Date().getTime()));
//                        iMissionService.saveFbTurntableRecord(fbTurntableRecord);
//                    }
//
//                }
//            }
//
//        logger.info(">>>>>>>>>>>>>>>>>>>turntableUser.............");
//    }














}
